package com.wdcg.model.dbhelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;


import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.wdcg.model.dao.Bonus;
import com.wdcg.model.dao.BonusDescription;
import com.wdcg.model.dao.BonusType;
import com.wdcg.model.dao.BonusTypeDescription;
import com.wdcg.model.dao.Dwarf;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.model.dao.PlayerLocale;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	private static DatabaseHelper databaseHelper;
	private static final String copySourcePath = "DwarfGameDB.sqlite";
	private static final String databaseName = "DwarfGameDB.sqlite";
	private static final String databasePath = "data/data/com.wdcg/databases/";
	private static final int databaseVersion = 1;
	private final Context context;

	public DatabaseHelper(Context context) {
		super(context, databaseName, null, databaseVersion);
		this.context = context;
	}

	private Dao<Dwarf, Integer> dwarfDao = null;
	private Dao<DwarfDescription, Integer> dwarfDescriptionDao = null;
	private Dao<Bonus, Integer> bonusDao = null;
	private Dao<BonusDescription, Integer> bonusDescriptionDao = null;
	private Dao<BonusType, Integer> bonusTypeDao = null;
	private Dao<BonusTypeDescription, Integer> bonusTypeDescriptionDao = null;
	private Dao<PlayerLocale, Integer> localeDao = null;

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
			int arg3) {
	}

	public void createDatabaseFromCopy() {

		this.getWritableDatabase();
		try {
			copyDatabase();
		} catch (IOException e) {
			Log.i(DatabaseHelper.class.getName(), "cant copy database");
			e.printStackTrace();
		}
		Log.i(DatabaseHelper.class.getName(), "database copied successfully");
		close();

	}

//	private boolean databaseExists() {
//
//		SQLiteDatabase checkDB = null;
//
//		try {
//			String myPath = databasePath + databaseName;
//			checkDB = SQLiteDatabase.openDatabase(myPath, null,
//					SQLiteDatabase.OPEN_READONLY);
//
//		} catch (SQLiteException e) {
//			e.printStackTrace();
//		}
//
//		if (checkDB != null) {
//
//			checkDB.close();
//
//		}
//
//		return checkDB != null ? true : false;
//	}

	private void copyDatabase() throws IOException {
		// Open your local db as the input stream
		InputStream myInput = context.getAssets().open(copySourcePath);

		// Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(databasePath
				+ databaseName);
		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	public void createEmptyDB() {
		try {
			TableUtils.createTable(connectionSource, Dwarf.class);
			TableUtils.createTable(connectionSource, DwarfDescription.class);
			TableUtils.createTable(connectionSource, Bonus.class);
			TableUtils.createTable(connectionSource, BonusType.class);
			TableUtils.createTable(connectionSource, BonusDescription.class);
			TableUtils.createTable(connectionSource, BonusTypeDescription.class);
			TableUtils.createTable(connectionSource, PlayerLocale.class);
			Log.i(DatabaseHelper.class.getName(), "database created successfully");
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			e.printStackTrace();
		}
	}

	public Dao<Dwarf, Integer> getDwarfDao() {
		if (null == dwarfDao) {
			try {
				dwarfDao = getDao(Dwarf.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return dwarfDao;
	}

	public Dao<DwarfDescription, Integer> getDwarfDescriptionDao() {
		if (null == dwarfDescriptionDao) {
			try {
				dwarfDescriptionDao = getDao(DwarfDescription.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return dwarfDescriptionDao;
	}

	public Dao<Bonus, Integer> getBonusDao() {
		if (null == bonusDao) {
			try {
				bonusDao = getDao(Bonus.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return bonusDao;
	}

	public Dao<BonusDescription, Integer> getBonusDescriptionDao() {
		if (null == bonusDescriptionDao) {
			try {
				bonusDescriptionDao = getDao(BonusDescription.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return bonusDescriptionDao;
	}

	public Dao<BonusType, Integer> getBonusTypeBao() {
		if (null == bonusTypeDao) {
			try {
				bonusTypeDao = getDao(BonusType.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return bonusTypeDao;
	}

	public Dao<BonusTypeDescription, Integer> getBonusTypeDescriptionDao() {
		if (null == bonusTypeDescriptionDao) {
			try {
				bonusTypeDescriptionDao = getDao(BonusTypeDescription.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return bonusTypeDescriptionDao;
	}

	public Dao<PlayerLocale, Integer> getPlayereLocaleDao() {
		if (null == localeDao) {
			try {
				localeDao = getDao(PlayerLocale.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return localeDao;
	}
	
	public static void initializeMainDBHelper(Context context){
		if(databaseHelper==null){
			databaseHelper=new DatabaseHelper(context);
		}
	}
	
	public static DatabaseHelper getMainDBHelper(){
		return databaseHelper;
	}
	
}
